package com.example.boot1.service.impl;

import com.example.boot1.util.SnowflakeIdWorker;
import com.example.boot1.util.StringHelp;
import com.example.boot1.util.ThreadPoolHelp;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.example.boot1.bean.VueUser;
import com.example.boot1.mapper.VueUserMapper;
import com.example.boot1.service.VueUserService;

import java.util.*;
import java.util.concurrent.*;

@Service
public class VueUserServiceImpl implements VueUserService{

    @Autowired
    private VueUserMapper vueUserMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return vueUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(VueUser record) {
        return vueUserMapper.insert(record);
    }

    @Override
    public int insertSelective(VueUser record) {
        return vueUserMapper.insertSelective(record);
    }

    @Override
    public VueUser selectByPrimaryKey(Integer id) {
        return vueUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(VueUser record) {
        return vueUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(VueUser record) {
        return vueUserMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<VueUser> findUserPage(Integer page) {
        return vueUserMapper.findUserPage(page);
    }

    @Override
    public int findAllTotal() {
        return vueUserMapper.findAllTotal();
    }

    @Override
    public void insertBig() {

        ExecutorService threadPool = ThreadPoolHelp.getThreadPool();
        //ExecutorService threadPool = Executors.newCachedThreadPool();

        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);

        try {
            for (int j = 1; j <=10000 ; j++) {
                List<Map> list= new ArrayList();
                for (int i = 1; i <=3000 ; i++) {
                    Map<String,Object> map= new HashMap();
                    map.put("name", StringHelp.generateRandomString(10));
                    map.put("orderId",idWorker.nextId());
                    list.add(map);
                }

                    threadPool.submit(()->{
                        try {
                            vueUserMapper.insertBig(list);
                        } catch (Exception e) {
                            System.out.println("数据库异常");
                        }
                    });
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            threadPool.shutdown();
        }
        //测试是否正确
//        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
//        List list= new ArrayList();
//        for (int i = 1; i <=2 ; i++) {
//            Map<String,Object> map= new HashMap();
//            map.put("name", StringHelp.generateRandomString(10));
//            map.put("orderId",idWorker.nextId());
//            list.add(map);
//        }
//        vueUserMapper.insertBig(list);

    }


}
